package com.ciwong.epaper.service;

import android.text.TextUtils;

import com.ciwong.epaper.util.ALiYunManager;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.epaper.util.FileUtil;

import java.io.File;
import java.util.List;

public class UploadServiceHelper {

    public static final String FILE_FLAGE_UPLOADED_ANSWER_CIONG = "uploadedanswer_ciwong";
    public static final String FILE_FLAGE_UPLOADED_DIR = "uploadeddir_";
    public static final String FILE_FLAGE_UPLOADED_FILE = "uploadedfile_";

    public static void checkDirIsEmpty(String answersPath) {
        File file = new File(answersPath);
        if (file.exists() && file.isDirectory() && file.list() != null && file.list().length > 0) {//当文件夹存在，里面有文件
            FileUtil.deleteEmptyDir(answersPath);

        }
    }

    public static void checkDirIsOnlyContainUploadedFlag(String answersPath) {
        File file = new File(answersPath);
        if (file.exists() && file.isDirectory() && file.list() != null && file.list().length > 0) {//当文件夹存在，里面有文件
            FileUtil.deleteOnlyContainUploadedFlag(answersPath, FILE_FLAGE_UPLOADED_ANSWER_CIONG);
        }
    }

    /**
     * 遍历Ansers 中的文件若有包含
     * {@link com.ciwong.epaper.service.UploadServiceHelper.FILE_FLAGE_UPLOADED_ANSWER_CIONG},没有重命名的，去 重命名
     *
     * @param answersPath
     */
    public static void renameDirContainUploadedFlag(String answersPath) {
        File file = new File(answersPath);
        if (file.exists() && file.isDirectory() && file.list() != null && file.list().length > 0) {//当文件夹存在，里面有文件
            FileUtil.renameDirContainUploadedFlag(answersPath, FILE_FLAGE_UPLOADED_ANSWER_CIONG);
        }
    }

    /**
     * 遍历上传失败文件（以.mp3结尾，文件夹包含“uploadeddir_”或者 文件夹内包含标记文件“uploadedanswer_ciwong”，文件包含“uploadedfile_”），
     * 即是，已经提交过作业的文件夹，已经上传过的音频文件
     * #param dirPath 要遍历文件夹路径
     */
    public static void traversalDeleteUpLoadedMP3s(String dirPath) {
        //文件中遗留的MP3 文件
        List<String> mp3Dirs = FileUtil.findMp3DirContainedSpecialChar(dirPath, FILE_FLAGE_UPLOADED_DIR);
        for (String dir : mp3Dirs) {
            deleteUpLoadedMP3s(dir);
        }
    }

    /**
     * 遍历Answer文件中未上传完成文件（以.mp3结尾，）
     */
    protected static void traversalUpLoadUnUPMP3s() {

        //文件中遗留的MP3 文件(包含上传与未上传)
        List<String> mp3File = FileUtil.findMp3s(ESystem.getAnswersNUploadPathCreate());
        for (String path : mp3File) {
            System.out.println("lzh==>> traversalUpLoadUnUPMP3s :" + path);
            if (!path.contains(FILE_FLAGE_UPLOADED_FILE))//只上传未标记上传成功的
                ALiYunManager.getInstance().addMP3(path, path);
        }
    }

    /**
     * 上传单个文件夹（遍历刚上传完成的文件夹）
     * 遍历上传重命名后未上传护着上传失败文件（以.mp3结尾）
     */
    protected static void upLoadDirMP3s(String dir) {
        //文件中遗留的MP3 文件
        List<String> mp3s = FileUtil.findMp3s(dir);
        if (mp3s == null || mp3s.size() <= 0) return;
        for (String path : mp3s) {
            System.out.println("lzh==>> upLoadDirMP3s :" + path);
            if (!path.contains(FILE_FLAGE_UPLOADED_FILE))//只上传未标记上传成功的
                ALiYunManager.getInstance().addMP3(path, path);
        }
    }

    /**
     * 遍历文件（以.mp3结尾,包含特殊字符）
     *
     * @param parentPath ex FILE_FLAGE_UPLOADED_DIR
     */
    protected static void deleteUpLoadedMP3s(String parentPath) {
        //文件中遗留的MP3 文件
        List<String> mp3s = FileUtil.findMp3s(parentPath);
        if (mp3s == null || mp3s.size() <= 0) return;
        for (String path : mp3s) {
            System.out.println("lzh==>> deleteUpLoadedMP3s :" + path);
            if (path.contains(FILE_FLAGE_UPLOADED_FILE)) {//只删除包含 标记FILE_FLAGE_UPLOADED_FILE的文件
                FileUtil.deleteFile(path);
            }
        }
    }

    /**
     * 便利上传 老版本保留的未提交作业的音频文件
     * @param uuid
     */
    public static void uploadOldVersionFile(String uuid) {
        if(TextUtils.isEmpty(uuid))return;
        List<String> oldVersionUuidDirs= FileUtil.findDirs(ESystem.getAnswersPath(),uuid);
        for (String oldVersionUuidDir:oldVersionUuidDirs){
            //文件中遗留的MP3 文件
//            System.out.println("lzh==> oldVersionUuidDir："+oldVersionUuidDir);
            upLoadDirMP3s(oldVersionUuidDir);
        }
    }
}
